import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

import 'index.dart';

class PrivacyPolicyPage extends GetView<PrivacyPolicyController> {
  const PrivacyPolicyPage({super.key});

  // 主视图
  Widget _buildView() {
    return Stack(
      children: [
        // WebView
        WebViewWidget(controller: controller.webViewController),
        
        // 加载进度
        Obx(() => controller.isLoading.value
          ? Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(
                      Color(0xFFCB5050),
                    ),
                  ),
                  const SizedBox(height: 16),
                  Obx(() => Text('加载中 ${controller.loadingProgress.value}%')),
                ],
              ),
            )
          : const SizedBox.shrink()
        ),
        
        // 错误显示
        Obx(() => controller.hasError.value
          ? Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Icon(Icons.error_outline, size: 48, color: Color(0xFFCB5050)),
                  const SizedBox(height: 16),
                  const Text('加载失败，请检查网络连接'),
                  const SizedBox(height: 8),
                  const Text(
                    '如果多次尝试仍然失败，请联系客服', 
                    style: TextStyle(fontSize: 12, color: Colors.grey),
                  ),
                  const SizedBox(height: 24),
                  TextButton(
                    onPressed: controller.reload,
                    style: TextButton.styleFrom(
                      backgroundColor: const Color(0xFFCB5050),
                      foregroundColor: Colors.white,
                      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                    ),
                    child: const Text('重新加载'),
                  ),
                ],
              ),
            )
          : const SizedBox.shrink()
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () => Get.back(),
          icon: const Icon(Icons.arrow_back_ios_new_outlined),
        ),
        title: Text(controller.title),
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
      ),
      body: SafeArea(
        child: _buildView(),
      ),
    );
  }
}
